package com.xzc.lease.web.admin.controller.login;


import com.xzc.lease.common.login.LoginUser;
import com.xzc.lease.common.login.LoginUserHolder;
import com.xzc.lease.common.result.Result;
import com.xzc.lease.web.admin.service.LoginService;
import com.xzc.lease.web.admin.vo.login.CaptchaVo;
import com.xzc.lease.web.admin.vo.login.LoginVo;
import com.xzc.lease.web.admin.vo.system.user.SystemUserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
@Slf4j
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        log.info("获取图形验证码");
        CaptchaVo result = loginService.getCaptcha();
        return Result.ok(result);
    }

    @Operation(summary = "登录")
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        log.info("登录请求：{}", loginVo);
        //登录成功后，返回jwt令牌
        String jwt = loginService.login(loginVo);
        return Result.ok(jwt);
    }

    @Operation(summary = "获取登陆用户个人信息")
    @GetMapping("info")
    //@RequestHeader：将http请求的某一个header绑定到该参数上
    public Result<SystemUserInfoVo> info(/*@RequestHeader("access-token") String token*/) {
        log.info("获取登陆用户个人信息");
        //通过ThreadLocal获取用户信息
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        SystemUserInfoVo result = loginService.getLoginUserInfoById(loginUser.getUserId());
        return Result.ok(result);
    }
}